package com.xiaomoyu.pz;

import com.xiaomoyu.pz.utils.Base64EncodeUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录
 */
public class LoginPz {
    public static void main(String[] args) {
        Map<String,String> param = new HashMap<>();
        String userAccount = "输入你的账户";
        String userPassword = "输入你的密码";
        String encoded = "";//加密信息
        try {
             encoded = Base64EncodeUtils.encryptBASE64(userAccount.getBytes()) +"%%%"+Base64EncodeUtils.encryptBASE64(userPassword.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        param.put("userAccount",userAccount);
         param.put("userPassword",userPassword);

        param.put("encoded",encoded);
        List<URI> redirectLocations = null;
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost("http://qzjw.peizheng.edu.cn/jsxsd/xk/LoginToXk");
            //请求头
            httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.addHeader("Cookie","application/x-www-form-urlencoded; charset=UTF-8");
            // 创建参数列表
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
                // 模拟表单登录
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
                httpPost.setEntity(entity);
            }

            // 执行http请求
            HttpClientContext context = HttpClientContext.create();
            response = httpClient.execute(httpPost,context);

            //获取Cookie信息,得到两个参数 JSESSIONID 、 BIGipServerpool_qzjw
            List<Cookie> cookies = context.getCookieStore().getCookies();
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println("name:"+name+","+value);
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }





    }
}
